package com.defysoft.waukids.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.defysoft.waukids.Custom_Data_Type.Bc_Student_Att;
import com.defysoft.waukids.PublicConstant;
import com.defysoft.waukids.R;
import com.defysoft.waukids.attend.Attend_SelectDay_A;

public class Adapter_AttendSelectMemeber_L extends ArrayAdapter<String> {

	ArrayList<Bc_Student_Att> list;
	LayoutInflater layoutInflater;

	public Adapter_AttendSelectMemeber_L(Context context, int resource,
			ArrayList<Bc_Student_Att> list, LayoutInflater layoutInflater) {
		super(context, 0);
		this.list = list;
		this.layoutInflater = layoutInflater;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		attendMemberHolder holder;
		if (convertView == null) {
			holder = new attendMemberHolder();
			convertView = layoutInflater.inflate(
					R.layout.attendselectmemeber_row, null);
			holder.tv[0] = (TextView) convertView
					.findViewById(R.id.ATTENDSELECTMEMBER_ROW_NAME);
			holder.tv[1] = (TextView) convertView
					.findViewById(R.id.ATTENDSELECTMEMBER_ROW_BIRTH);
			holder.tv[2] = (TextView) convertView
					.findViewById(R.id.ATTENDSELECTMEMBER_ROW_CLASS);
			holder.wv = (WebView) convertView
					.findViewById(R.id.ATTENDSELECTMEMBER_ROW_IMG);
			holder.iv = (ImageView) convertView
					.findViewById(R.id.ATTENDSELECTMEMBER_ROW_ATTEND);
			holder.ll = (LinearLayout) convertView
					.findViewById(R.id.ATTENDSELECTMEMBER_ROW_LL);
			convertView.setTag(holder);
		} else {
			holder = (attendMemberHolder) convertView.getTag();
		}
		
		if (list.get(position).check.equals("null")) {
			holder.tv[0].setTextColor(0xffaaaaaa);
			holder.tv[1].setTextColor(0xffaaaaaa);
			holder.tv[2].setTextColor(0xffaaaaaa);
			holder.ll.setBackgroundResource(R.drawable.attenditem_false);
			/*holder.iv.setBackgroundResource(R.drawable.at_off);*/
		} else if (list.get(position).check.equals("0")) {
			holder.tv[0].setTextColor(0xffaaaaaa);
			holder.tv[1].setTextColor(0xffaaaaaa);
			holder.tv[2].setTextColor(0xffaaaaaa);
			holder.ll.setBackgroundResource(R.drawable.attenditem_false);
			holder.iv.setBackgroundResource(R.drawable.at_on);
		} else {
			holder.tv[0].setTextColor(0xff000000);
			holder.tv[1].setTextColor(0xff000000);
			holder.tv[2].setTextColor(0xff000000);
			holder.ll.setBackgroundResource(R.drawable.attenditem_true);
			holder.iv
					.setBackgroundResource(R.drawable.at_off);
		}
		
		holder.wv.setVerticalScrollBarEnabled(false);
		holder.wv.setVerticalScrollbarOverlay(false);
		holder.wv.setHorizontalScrollBarEnabled(false);
		holder.wv.setHorizontalScrollbarOverlay(false);
		holder.wv.setInitialScale(100);

		if ( list.get(position).Stu_Photo == null||list.get(position).Stu_Photo.length()<5) {// 이미지 파일이 없으면 앱내에서 지정된 이미지를 뿌려주고
			holder.wv.setBackgroundColor(0);
			holder.wv.setBackgroundResource(R.drawable.perm_group_personal_info);
		} else {// 이미지 파일이 있으면
			holder.wv.loadDataWithBaseURL(null,
					creHtmlBody(PublicConstant.URI_STUDENDPHOTO
							+ list.get(position).Stu_Photo), "text/html", "utf-8", null);
		}
		
		holder.tv[0].setText(list.get(position).Stu_Name);
		holder.tv[1].setText(list.get(position).Stu_Birth);
		holder.tv[2].setText(list.get(position).cla_name);
		final int pos = position;
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				getContext()
						.startActivity(
								new Intent(getContext(),
										Attend_SelectDay_A.class)
										.putExtra(
												PublicConstant.INTENT_EXTRA_ATTEND_PK_STU_CODE,
												list.get(pos).PK_Stu_Code + "")
										.putExtra(
												PublicConstant.INTENT_EXTRA_ATTEND_STU_NAME,
												list.get(pos).Stu_Name + ""));
			}
		});

		return convertView;
	}

	class attendMemberHolder {
		ImageView iv;
		WebView wv;
		TextView tv[] = new TextView[3];
		LinearLayout ll;
	}
	
	public String creHtmlBody(String imagUrl) {
		StringBuffer sb = new StringBuffer("<HTML>");
		sb.append("<HEAD>");
		sb.append("</HEAD>");
		sb.append("<BODY  style='margin: 0; padding: 0'>");
		sb.append("<img width=\"100%\" height=\"100%\" src=\"" + imagUrl
				+ "\">");
		sb.append("</BODY>");
		sb.append("</HTML>");

		return sb.toString();
	}
}
